<div class="alert">
<?php
require_once('includes/user_manage.php');

$user->isAdmin or exit("Área reservada a administradores");

if (isset($_POST["name"])) {
	$my_category=new admin_category();
	$my_category->name=$_POST["name"];
	$my_category->parent_id=$_POST["parent_category_id"];
	$my_category->sort_order=$_POST["sort_order"];
	$inserted=$my_category->db_insert();
	if ($inserted) {
		$my_category->db_load();
		$serCategory=json_encode($my_category);
?>
	Se ha añadido la categoria.
	<form id="myAddForm"><input type="hidden" value='<?php print htmlentities($serCategory); ?>' name="serCategory"></form>
<?php
	}
	else {
?>
	Se ha producido un error.
<?php
	}
?>
<script type="text/javascript">
var categoryId=<?php print $_POST["parent_category_id"]; ?>;

var serCategory=document.getElementById("myAddForm").serCategory.value;
	var catalogPointer=myCatalog;
	if (categoryId>0) {
		catalogPointer=catalogPointer.moveToChildren("categoryId", categoryId);
	}

var myCatalogNode=new adminCatalogNode();
myCatalogNode.parentNode=catalogPointer;
myCatalogNode.myCategory.setFromArray(JSON.parse(serCategory));
catalogPointer.children.push(myCatalogNode);
	if (catalogPointer.myCategory.id>0) {
		catalogPointer.refreshCategoriesListView("flaps");
	}
	else {
		catalogPointer.refreshCategoriesListView("categories");
	}
	catalogPointer=myCatalogNode;
	catalogPointer.myCategory.myActivate();
	catalogPointer.myCategory.myActionButton.request();

window.setTimeout(removeAlert,2000,document.getElementById('addCategory'));
function removeAlert(alert) {
	alert.parentNode.removeChild(alert);
}
</script>
<?php
}
else {
?>
	<form method="post" action="?section=add_category" myFor="addCategory" id="addCategoryForm">
	<input type="hidden" value="" name="sort_order">
	<input type="hidden" value="<?php print $_GET["parent_category_id"]; ?>" name="parent_category_id">
	<table class="mytable">
		<tr>
			<td colspan="2" id="myMsg">Crear nueva categoría</td>
		</tr>
		<tr>
			<td colspan="2"><label>Nombre: <input type="text" name="name" value=""></label></td>
		</tr>
		<tr>
			<td><input type="submit" value="Aceptar" name="submitButton"></td>
			<td><input type="button" value="Cerrar" myFor="addCategory"></td>
		</tr>
	</table>
	</form>
<script type="text/javascript">
var categoryId=<?php print $_GET["parent_category_id"]; ?>;

	var catalogPointer=myCatalog;
	if (categoryId>0) {
		catalogPointer=catalogPointer.moveToChildren("categoryId", categoryId);
	}

var maxOrder=0;

for (var i in catalogPointer.children) {
	if (catalogPointer.children[i].myCategory.sort_order > maxOrder) maxOrder=catalogPointer.children[i].myCategory.sort_order;
}

var sortOrder=Number(maxOrder) + 1;
var myForm=document.getElementById("addCategoryForm");
myForm.sort_order.value=sortOrder;
myForm.name.focus();

if (catalogPointer.items.length>0) {
	var alertMsg =" La categoría contiene productos. Borre primero los productos.";
	document.getElementById("myMsg").innerHTML=alertMsg;
	var myForm=document.getElementById("addCategoryForm");
	myForm.submitButton.disabled=true;
}
</script>
<?php
}
?>
</div>